const webpack = require('webpack');
const path = require('path');
const merge = require('webpack-merge');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const baseConfig = require('./webpack.base');
const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin');
const config = require('./config')
const portfinder = require('portfinder');

const devConfig = merge.smart(baseConfig, {
    mode: 'development',
    devtool: 'source-map',
    output: {
        // 开发环境下，不能使用 contenthash/chunkhash
        filename: '[name].[hash:8].js',
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: config.indexPath,
            minify: {
                html5: true
            },
            hash: false
        }),
        new FriendlyErrorsPlugin(),
        new webpack.HotModuleReplacementPlugin(),
    ],
    devServer: {
        ...config.devServer
    },
    stats: {
        colors: true,
        children: false,
        chunks: false,
        chunkModules: false,
        modules: false,
        builtAt: false,
        entrypoints: false,
        assets: false,
        version: false
    }
});


module.exports = new Promise((resolve, reject) => {

    portfinder.basePort = config.devServer.port;
    portfinder.getPort((err, port) => {
        if (err) reject(err)
        else {
            devConfig.devServer.port = port;
        }
        resolve(devConfig)
    })

})

